package com.cxh.rmsq.ui.activity.self;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.cxh.rmsq.R;
import com.cxh.rmsq.common.IntentExtra;
import com.cxh.rmsq.model.Resource;
import com.cxh.rmsq.model.Status;
import com.cxh.rmsq.ui.activity.TitleBaseActivity;
import com.cxh.rmsq.ui.dialog.CommonDialog;
import com.cxh.rmsq.utils.ToastUtils;
import com.cxh.rmsq.viewmodel.UserDetailViewModel;

import java.util.Objects;

/**
 * 好友详情右上角更多 点击的页面
 */
public class UserOperateActivity extends TitleBaseActivity implements View.OnClickListener {

    private UserDetailViewModel userDetailViewModel;
    private String userId;
    private TextView tv_report;
    private TextView blacklistSiv;
    private boolean isInBlackList = false;
    /**
     * 是否在删除好友操作，如果在删除中则不更新好友状态，防止在删除时会有界面刷新
     */
    private boolean isInDeleteAction = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_operate);
        userId = getIntent().getStringExtra(IntentExtra.STR_TARGET_ID);
        userDetailViewModel =
                ViewModelProviders.of(
                                this, new UserDetailViewModel.Factory(getApplication(), userId))
                        .get(UserDetailViewModel.class);
        getTitleBar().getTvTitle().setText("更多");
        blacklistSiv = findViewById(R.id.tv_black);
        blacklistSiv.setOnClickListener(this);
        tv_report = findViewById(R.id.tv_report);
        tv_report.setOnClickListener(this);

        if (Objects.equals(getIntent().getStringExtra("fromWhere"), "community")) {
            tv_report.setText("社区内容举报");
        } else {
            tv_report.setText("举报");
        }
        initData();
    }

    private void initData() {
        // 获取黑名单状态
        userDetailViewModel
                .getIsInBlackList()
                .observe(
                        this,
                        new Observer<Boolean>() {
                            @Override
                            public void onChanged(Boolean isInBlackList) {
                                if (!isInDeleteAction) {
                                    updateBlackListItem(isInBlackList);
                                }
                            }
                        });

        // 获取添加到黑名单结果
        userDetailViewModel
                .getAddBlackListResult()
                .observe(
                        this,
                        new Observer<Resource<Void>>() {
                            @Override
                            public void onChanged(Resource<Void> resource) {
                                if (!isInDeleteAction) {
                                    if (resource.status == Status.SUCCESS) {
                                        ToastUtils.showToast(R.string.common_add_successful);
                                    } else if (resource.status == Status.ERROR) {
                                        ToastUtils.showToast(resource.message);
                                    }
                                }
                            }
                        });

        // 获取移除黑名单结果
        userDetailViewModel
                .getRemoveBlackListResult()
                .observe(
                        this,
                        new Observer<Resource<Void>>() {
                            @Override
                            public void onChanged(Resource<Void> resource) {
                                if (resource.status == Status.SUCCESS) {
                                    ToastUtils.showToast(R.string.common_remove_successful);
                                } else if (resource.status == Status.ERROR) {
                                    ToastUtils.showToast(resource.message);
                                }
                            }
                        });
        // 获取删除好友
        userDetailViewModel
                .getDeleteFriendResult()
                .observe(
                        this,
                        new Observer<Resource<Void>>() {
                            @Override
                            public void onChanged(Resource<Void> resource) {
                                if (resource.status == Status.SUCCESS) {
                                    ToastUtils.showToast(R.string.common_delete_successful);
                                    // 删除成功后关闭界面
                                    finish();
                                } else if (resource.status == Status.ERROR) {
                                    // 删除失败时置回删除行为
                                    isInDeleteAction = false;
                                    ToastUtils.showToast(resource.message);
                                }
                            }
                        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_black:
                toBlackList(!isInBlackList);
                break;
            case R.id.tv_report:
                startActivity(new Intent(this, ReportActivity.class)
                        .putExtra("fromWhere", getIntent().getStringExtra("fromWhere"))
                        .putExtra("contentId", getIntent().getStringExtra("contentId"))
                        .putExtra(IntentExtra.STR_TARGET_ID, userId));
                break;
        }
    }


    /**
     * 刷新更多中黑名单选项
     *
     * @param isInBlackList
     */
    private void updateBlackListItem(boolean isInBlackList) {
        this.isInBlackList = isInBlackList;
        if (isInBlackList) {
            blacklistSiv.setText(R.string.profile_detail_remove_from_blacklist);
        } else {
            blacklistSiv.setText(R.string.profile_detail_join_the_blacklist);
        }
    }

    /**
     * 是否加到黑名单
     *
     * @param isToBlack true 代表加到黑名单，false 代表移除掉黑名单
     */
    private void toBlackList(boolean isToBlack) {
        if (isToBlack) {
            // 显示确认对话框
            CommonDialog commonDialog =
                    new CommonDialog.Builder()
                            .setContentMessage(getString(R.string.profile_add_to_blacklist_tips))
                            .setDialogButtonClickListener(
                                    new CommonDialog.OnDialogButtonClickListener() {
                                        @Override
                                        public void onPositiveClick(View v, Bundle bundle) {
                                            userDetailViewModel.addToBlackList();
                                        }

                                        @Override
                                        public void onNegativeClick(View v, Bundle bundle) {
                                        }
                                    })
                            .build();
            commonDialog.show(getSupportFragmentManager(), null);
        } else {
            userDetailViewModel.removeFromBlackList();
        }
    }

}